package edu.ubb.bibliospring.rmiclient;

import edu.ubb.bibliospring.backend.model.Author;
import edu.ubb.bibliospring.backend.service.AuthorService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.BasicConfigurator;

public class Main {

    public static void main(String... args) {
        BasicConfigurator.configure();
        final Logger logger = LoggerFactory.getLogger(Main.class);

        try {
            System.out.println("Rmi example application started!");
            logger.info("Rmi example application started!");

            final InitialContext initialContext = new InitialContext();
            System.out.println("InitialContext created");
            logger.debug("InitialContext created");

            System.out.println("Get AuthorService ....");
            logger.debug("Get AuthorService ....");
            final AuthorService authorService = (AuthorService) initialContext.lookup("java:global/biblio-spring-server/biblio-spring-backend-1.0-SNAPSHOT/AuthorService");
            System.out.println("AuthorService accessed");
            logger.debug("AuthorService accessed");

            final Author author = new Author("Szecsi", "Balazs2");
            authorService.insertAuthor(author);
            logger.info("Author inserted");
            System.out.println("Author inserted");

            System.out.println("Get all author from database....");
            logger.debug("Get all author from database....");
            List<Author> authors = authorService.getAll();
            System.out.println("Authors in from databse:");
            logger.debug("Authors in from databse:");
            for (Author item : authors) {
                System.out.println(item.getFirstName() + " " + item.getLastName());
                logger.debug(item.getFirstName() + " " + item.getLastName());
            }
        } catch (final NamingException e) {
            logger.error("Something went wrong, the exception is:", e);
            e.printStackTrace();
        }
    }
}
